﻿using Dtmcli;
using Microsoft.AspNetCore.Mvc;
using MQCommon.SagaModel;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace SagaAggregateServices.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CtreatOrderController : ControllerBase
    {
        private readonly IDtmClient _dtmClient;
        private readonly IDtmTransFactory _transFactory;
        public CtreatOrderController( IDtmClient dtmClient, IDtmTransFactory transFactory)
        {
            _dtmClient = dtmClient;
            _transFactory = transFactory;
        }
        // GET: api/<CtreatOrderController>
        [HttpPost("CtreatOrder")]
        public async Task<IActionResult> CtreatOrder()
        {
            var cancellationToken = new CancellationToken();
            var gid = await _dtmClient.GenGid(cancellationToken);
            OrderModel body=new OrderModel();
            body.Id = 1;
            body.UserId = 1;
            body.BusinessID = 1;
            body.Money = 1;
            body.UserName = "22";
            body.BusinessName = "33";
            //下面add是按照顺序执行，当然也可以设置并行详细请看官网
            var saga = _transFactory.NewSaga(gid)
                .Add("http://175.178.81.27:5010/api/ctreatUser", "http://175.178.81.27:5010/api/ctreatUsererror", body)
                .Add("http://119.29.92.184:5010/api/ctreatBusinessAccounts", "http://119.29.92.184:5010/api/ctreatBusinessAccountserror", body)
                ;

            await saga.Submit(cancellationToken);
            return Ok(TransResponse.BuildSucceedResponse());

        }
        [HttpPost("CtreatOrderOne")]
        public async Task<IActionResult> CtreatOrderOne()
        {
            var cancellationToken = new CancellationToken();
            var gid = await _dtmClient.GenGid(cancellationToken);
            OrderModel body = new OrderModel();
            body.Id = 1;
            body.UserId = 1;
            body.BusinessID = 1;
            body.Money = 1;
            body.UserName = "22";
            body.BusinessName = "33";
            var saga = _transFactory.NewSaga(gid)
                .Add("http://175.178.81.27:5010/api/ctreatUserOne", "http://175.178.81.27:5010/api/ctreatUsererror", body)
                .Add("http://119.29.92.184:5010/api/ctreatBusinessAccounts", "http://119.29.92.184:5010/api/ctreatBusinessAccountserror", body)
                ;

            await saga.Submit(cancellationToken);
            return Ok(TransResponse.BuildSucceedResponse());

        }
        [HttpPost("CtreatOrderTwo")]//空补偿
        public async Task<IActionResult> CtreatOrderTwo()
        {
            var cancellationToken = new CancellationToken();
            var gid = await _dtmClient.GenGid(cancellationToken);
            OrderModel body = new OrderModel();
            body.Id = 1;
            body.UserId = 1;
            body.BusinessID = 1;
            body.Money = 1;
            body.UserName = "22";
            body.BusinessName = "33";
            var saga = _transFactory.NewSaga(gid)
                .Add("http://175.178.81.27:5010/api/ctreatUserTwo", "http://175.178.81.27:5010/api/ctreatUsererror", body)
                .Add("http://119.29.92.184:5010/api/ctreatBusinessAccounts", "http://119.29.92.184:5010/api/ctreatBusinessAccountserror", body)
                ;

            await saga.Submit(cancellationToken);
            return Ok(TransResponse.BuildSucceedResponse());

        }
        [HttpPost("CtreatOrderthree")]
        public async Task<IActionResult> CtreatOrderthree()
        {
            var cancellationToken = new CancellationToken();
            var gid = await _dtmClient.GenGid(cancellationToken);
            OrderModel body = new OrderModel();
            body.Id = 1;
            body.UserId = 1;
            body.BusinessID = 1;
            body.Money = 1;
            body.UserName = "22";
            body.BusinessName = "33";
            var saga = _transFactory.NewSaga(gid)
                .Add("http://175.178.81.27:5010/api/ctreatUser", "http://175.178.81.27:5010/api/ctreatUsererrorOne", body)
                .Add("http://119.29.92.184:5010/api/ctreatBusinessAccounts", "http://119.29.92.184:5010/api/ctreatBusinessAccountserror", body)
                ;

            await saga.Submit(cancellationToken);
            return Ok(TransResponse.BuildSucceedResponse());

        }

    }
}
